package com.example.qa_back.service;

import com.example.qa_back.entity.CollectQuestion;
import com.example.qa_back.entity.IndexAndSimilarity;
import com.example.qa_back.entity.QA;
import com.example.qa_back.mapper.BackCQMapper;
import com.example.qa_back.mapper.BackQAMapper;
import com.example.qa_back.utils.ConnectPython;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 董帅
 * @date 2021/3/13 - 11:07
 */
@Service
public class FrontService {
    private final BackQAMapper backQAMapper;
    private final BackCQMapper backCQMapper;

    public FrontService(BackQAMapper backQAMapper, BackCQMapper backCQMapper) {
        this.backQAMapper = backQAMapper;
        this.backCQMapper = backCQMapper;
    }

    /*
    * 功能：得到所有的QA对
    * */
    public QA[] getAllQA() {
        return backQAMapper.getAllQA();
    }

    /*
    * 功能：返回答案
    * */
    public Object returnAnswer(String question) {
        QA[] allQA = getAllQA();
        StringBuilder str = new StringBuilder();
        str.append(question);
        str.append(" ");
        for (int i = 0; i < allQA.length; ++i) {
            str.append(allQA[i].getQuestion());
            if (i != allQA.length - 1)
                str.append(" ");
        }
        ArrayList<Double> list = ConnectPython.execPython(str.toString());
        if (list.size() != 0) {
            IndexAndSimilarity[] allSimilarity = new IndexAndSimilarity[allQA.length];
            for (int i = 0; i < allQA.length; ++i) {
                IndexAndSimilarity indexAndSimilarity = new IndexAndSimilarity();
                indexAndSimilarity.setIndex(i);
                indexAndSimilarity.setSimilarity(list.get(i));
                allSimilarity[i] = indexAndSimilarity;
            }
            for (IndexAndSimilarity indexAndSimilarity : allSimilarity) {
                System.out.println(indexAndSimilarity.getIndex() + " " + indexAndSimilarity.getSimilarity());
            }
            System.out.println("---------------------------------");
            Arrays.sort(allSimilarity, (o1, o2) -> -o1.getSimilarity().compareTo(o2.getSimilarity()));
            for (IndexAndSimilarity indexAndSimilarity : allSimilarity) {
                System.out.println(indexAndSimilarity.getIndex() + " " + indexAndSimilarity.getSimilarity());
            }
            return allQA[allSimilarity[0].getIndex()];
        } else {
            return "error";
        }
    }


    /***
     * 功能：返回答案，使用的是深度学习模型
     * @param question 前端发送的问题
     */
    public Object returnAnswerDP(String question) {
        QA[] allQA = getAllQA();
        StringBuilder str = new StringBuilder();
        str.append(question);
        str.append(" ");
        for (int i = 0; i < allQA.length; ++i) {
            str.append(allQA[i].getQuestion());
            if (i != allQA.length - 1)
                str.append(" ");
        }
        List<Double> list = ConnectPython.execPythonDP(str.toString());
        if (list.size() != 0) {
            IndexAndSimilarity[] allSimilarity = new IndexAndSimilarity[allQA.length];
            for (int i = 0; i < allQA.length; ++i) {
                IndexAndSimilarity indexAndSimilarity = new IndexAndSimilarity();
                indexAndSimilarity.setIndex(i);
                indexAndSimilarity.setSimilarity(list.get(i));
                allSimilarity[i] = indexAndSimilarity;
            }
            for (IndexAndSimilarity indexAndSimilarity : allSimilarity) {
                System.out.println(indexAndSimilarity.getIndex() + " " + indexAndSimilarity.getSimilarity());
            }
            System.out.println("---------------------------------");
            Arrays.sort(allSimilarity, (o1, o2) -> -o1.getSimilarity().compareTo(o2.getSimilarity()));
            for (IndexAndSimilarity indexAndSimilarity : allSimilarity) {
                System.out.println(indexAndSimilarity.getIndex() + " " + indexAndSimilarity.getSimilarity());
            }
            return allQA[allSimilarity[0].getIndex()];
        } else {
            return "error";
        }
    }


//    public Object returnAnswer(String question) {
//        QA[] allQA = getAllQA();
//        String[] questions = new String[allQA.length];
//        for (int i = 0; i < questions.length; ++i) {
//            questions[i] = allQA[i].getQuestion();
//        }
//        ArrayList<Double> list = HandlePython.execPython(question, questions);
//        if (list.size() != 0) {
//            IndexAndSimilarity[] allSimilarity = new IndexAndSimilarity[allQA.length];
//            for (int i = 0; i < allQA.length; ++i) {
//                IndexAndSimilarity indexAndSimilarity = new IndexAndSimilarity();
//                indexAndSimilarity.setIndex(i);
//                indexAndSimilarity.setSimilarity(list.get(i));
//                allSimilarity[i] = indexAndSimilarity;
//            }
//            Arrays.sort(allSimilarity, (o1, o2) -> -o1.getSimilarity().compareTo(o2.getSimilarity()));
//            if (allSimilarity[0].getSimilarity() >= 0.7) {
//                return allQA[allSimilarity[0].getIndex()];
//            } else {
//                insertCQ(question);
//                return "no suitable answer";
//            }
//        } else {
//            return "some words don't have vector";
//        }
//
//    }

    /*
    * 功能：将问题加入到数据库中
    * */
    public void insertCQ(String question) {
        CollectQuestion collectQuestion = new CollectQuestion();
        collectQuestion.setQuestion(question);
        backCQMapper.insert(collectQuestion);
    }
}
